using System.Collections;
using DG.Tweening;
using QFramework;
using UnityEngine;
using UnityEngine.UI;
using static ChouseType;

public class TimeManager : MonoBehaviour
{
    //绑定 倒计时显示的对象
    public Text timeText;

    public RectTransform processBarRectTransform;

    //倒计时时间（秒）
    private int countDownTime = 20;
    private string timeModel = "20秒"; //玩家选择的倒计时时间模型

    private Coroutine countdownCoroutine;
    private Tween processBarTween;

    void Awake()
    {
         StringEventSystem.Global.Register(EventID.GameStarted, (GameType gameType) =>
            {
                timeModel = gameType.AnsTime;
                HandleStartTimeDown(timeModel);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

         StringEventSystem.Global.Register(EventID.NextTi, () =>{
            HandleStartTimeDown(timeModel);
        }).UnRegisterWhenGameObjectDestroyed(gameObject);

        //监听玩法结束画面
        StringEventSystem.Global.Register(EventID.GameOver, () =>
        {
            // 结束所有倒计时
            if (countdownCoroutine != null)
            {
                StopCoroutine(countdownCoroutine); // 停止当前的倒计时协程
                countdownCoroutine = null; // 重置协程引用
            }

            // 重置进度条
            if (processBarTween != null)
            {
                processBarTween.Kill(); // 停止并清除当前的 Tween 动画
                processBarRectTransform.sizeDelta = new Vector2(540, processBarRectTransform.sizeDelta.y); // 重置进度条大小
            }

            // 更新 UI 显示
            timeText.text = "0s"; // 显示倒计时结束
            Debug.Log("玩法结束，所有倒计时已停止！");
        }).UnRegisterWhenGameObjectDestroyed(gameObject);
    }
    void Start()
    {
        //获取player的倒计时时间
        timeModel = PlayerPrefs.GetString(GameMode.AnsTimeGroup);

        //在函数中将timenodel转为int类型
        countDownTime = ChouseType.AnsTimePrase(timeModel);
        //显示倒计时时间
        timeText.text = countDownTime.ToString() + "s";

    }

    private void HandleStartTimeDown(string count){
        processBarRectTransform.sizeDelta = new Vector2(540, processBarRectTransform.sizeDelta.y);
        countDownTime = ChouseType.AnsTimePrase(count);
         //写一个tween动画，针对processbar
        processBarTween = processBarRectTransform.DOSizeDelta(new Vector2(0, processBarRectTransform.sizeDelta.y), countDownTime)
            .SetEase(Ease.Linear);
        // 停止当前正在运行的倒计时协程
        if (countdownCoroutine != null)
        {
            StopCoroutine(countdownCoroutine);
        }
        // 启动新的倒计时协程
        countdownCoroutine = StartCoroutine(DownTime(countDownTime));
    }
    void Update()
    {
        if(TimeStop.isTimeStop){
            processBarTween.Pause();
        }else{
            processBarTween.Play();
        }
    }

    private IEnumerator DownTime(int countDownTime)
    {
        while (countDownTime > 0)
        {
            timeText.text = countDownTime.ToString() + "s";
            yield return new WaitForSeconds(1);
            if (!TimeStop.isTimeStop)
            {
                countDownTime--;
                
            }
            
        }
        //当倒计时结束时，执行相应操作
        Debug.Log("倒计时结束！");
        timeText.text = "0s";
        //发出一个事件，通知其他脚本倒计时结束
        StringEventSystem.Global.Send(EventID.TimeOver);
        countdownCoroutine = null;
    }

    // Update is called once per frame
}
